
/**
 * 移动端
 */
$(document).ready(function() {

	startBanner();

	$('.draggable-div .drag-card').draggable({
			connectToSortable: '.content-drag',
			helper: "clone",
			revert: "invalid",
			appendTo:"body",
			zIndex:999,
			start: function(e,t) {
					//开始移动
			},
			drag: function(e, t) {
					//移动过程
			},
			stop: function(e, t) {
					//移动结束 添加代码到目的div
					let id = t.helper.get(0).firstElementChild.id;
					// console.log('当前div id=' + id);
					setDivIds(id);


			}
	});

	$( ".content-drag" ).sortable({
			revert: true
	});

});

//设置ID
function setDivIds(id) {
	const divId = $(".content-drag #" + id);
	const ids = id + "-" + randomNumber();
	divId.attr("id", ids);

	//监听右键的事件
	$('#' + ids).mousedown(function(event) {
			if (event.which == 3) {
					// console.log("右键删除 id=" + ids);
					deleteDiv(ids);
			}
	});
}

//删除添加的组件
function deleteDiv(ids) {
	if (confirm("你确定删除吗？")) {
			//是的  删除
			$(".content-drag").get(0).removeChild(
					$('#' + ids).parent().get(0)
			);
	}
}

function randomNumber() {
	return Math.floor(Math.random() * 1e6 + 1);
}

/**
* 生成html
*/
function showHTML(){
	let e = $(".content-drag").html();

	$("#htmlText").val(e);
}


// 修改弹出框的title, 显示弹框
let domCss;
function ShowCreateModal(htmlCss){
	$('.card2,.rowNum').hide()
	if (typeof htmlCss == 'string') {
			$('.rowNum').show()
	} else {
			$('.card2').show()
	}
	domCss = htmlCss
	$('#setListItem').modal('show');
}

// 关闭弹框， 获取输入值，然后执行逻辑
$("#createFileSureBut").click(function (){
	if (typeof domCss == 'string') {
			let number = $("#fileName").val();
			new card2List(number, domCss)
	} else {
			let img = domCss.querySelector('i'), oSpan =  domCss.querySelector('span')
			img.style.backgroundImage = $('.upload').css('backgroundImage')
			oSpan.innerHTML = $('#listName').val()
	}
	$('#setListItem').modal('hide');
});

// card2 列表
function card2List(num, htmlCss) {
	this.num = num
	this.htmlCss = htmlCss
	this.card2List_setStyle()
}

card2List.prototype.card2List_setStyle = function() {
	let str = '<div class="card2_list">';
	for (let i = 0; i < this.num; i++) {
			str +=
			`<a class="card2_list_item" href="javascript:;" >
					<i></i>
					<span>中老年健康</span>
			</a>`
	}
	str += '</div>'
	$(this.htmlCss).append(str)
	$('.card2_list').mousedown(removeDom)
	// $('.card2_list_item').bind({
	//     'click': function() {
	//         ShowCreateModal(this)
	//     }
	// })
}


//上传
$('.upload input').change(function(e) {
	if (!e.target.files.length) return false;
	var file = e.target.files[0], reader = new FileReader();
	reader.readAsDataURL(file);
	reader.onload = function (e) { 
			$('.upload').css({backgroundImage: `url(${reader.result})`})
	}
})

//删除
var removeDom = function(event) {
	if (event.which == 3) {
			if (confirm("你确定删除吗？")) {
					//是的  删除
					$(this).remove();
			}
	}
}